package griffin.website;

import java.util.regex.Matcher;
import android.content.pm.ActivityInfo;
import java.util.regex.Pattern;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
//import android.content.Context;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class register extends Activity {

	private static final String SOAP_ACTION = "http://tempuri.org/register";
	private static final String METHOD_NAME = "register";
	private static final String NAMESPACE = "http://tempuri.org/";
	private static final String URL = "http://griffin.ciandt.cn/Sservice1.asmx";
	private EditText editUsername;
	private EditText editPassword;
	private EditText editCompassword;
	private EditText editEmail;
	private TextView lblError;
	private Button btnCancel;
	private Button btnRegister;
	/*private ScrollView sView;
	private final Handler mHandler=new Handler();
	private LinearLayout mLayout;*/

	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.register);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		
		editUsername=(EditText)findViewById(R.id.regusername);
		editPassword=(EditText)findViewById(R.id.regpassword);
		editCompassword=(EditText)findViewById(R.id.regcomfirmpassword);
		editEmail=(EditText)findViewById(R.id.regemail);
		btnRegister=(Button)this.findViewById(R.id.regregister);
		btnCancel=(Button)this.findViewById(R.id.regcancle);
		//sView=(ScrollView)this.findViewById(R.id.sclvRegister);	
//		Animation animation = AnimationUtils.loadAnimation(this, R.anim.shake);//animation
//		editUsername.startAnimation(animation);
//		editPassword.startAnimation(animation);
//		editCompassword.startAnimation(animation);
//		editEmail.startAnimation(animation);
		
		btnRegister.setOnClickListener(new Button.OnClickListener()
		{
			
			public void onClick(View v) {
				try{
					String username= editUsername.getText().toString();
					String password = editPassword.getText().toString();
					String compassword = editCompassword.getText().toString();
					String email = editEmail.getText().toString();
					
					if(compassword.compareTo(password)==0)
					{
						//validate
						if(username.length()==0||password.length()==0||email.length()==0)
						{
							Toast.makeText(register.this,"Please input Username,Password,Email!",Toast.LENGTH_SHORT).show();
							if(username.length()==0){
								Animation animation = AnimationUtils.loadAnimation(v.getContext(), R.anim.shake);//animation
								editUsername.startAnimation(animation);
							}else if(password.length()==0){
								Animation animation = AnimationUtils.loadAnimation(v.getContext(), R.anim.shake);//animation
								editPassword.startAnimation(animation);
							}else if(email.length()==0){
								Animation animation = AnimationUtils.loadAnimation(v.getContext(), R.anim.shake);//animation
								editEmail.startAnimation(animation);
							}
							return;
						}
						
						Pattern pattern = Pattern.compile("[\\w\\.\\-]+@([\\w\\-]+\\.)+[\\w\\-]+",Pattern.CASE_INSENSITIVE);   
						Matcher matcher = pattern.matcher(email);   
						if(!matcher.matches())
						{							
							Toast.makeText(register.this,"Please input corrot Email address!",Toast.LENGTH_SHORT).show();

							return;
						}
						//submit to server
						SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

						request.addProperty("username", username);
						request.addProperty("password", password);
						request.addProperty("email", email);

						SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
						envelope.dotNet=true;
						envelope.setOutputSoapObject(request);

						HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
						androidHttpTransport.call(SOAP_ACTION, envelope);

						Object result = (Object)envelope.getResponse();
						int userID = Integer.parseInt(result.toString());
						if(userID==0)
						{							
							Toast.makeText(register.this,"Network problem,Please try again!",Toast.LENGTH_SHORT).show();

						}
						else
						{
							SharedPreferences loginInfo=getSharedPreferences("griffin.website.loginInfo", 0);
							SharedPreferences.Editor editor=loginInfo.edit();
							editor.putString("username", username);
							editor.putInt("username", userID);
							editor.commit();
							register.this.finish();
						}
					}
					else
					{
						Toast.makeText(register.this,"Please input same password!",Toast.LENGTH_SHORT).show();
					}
					//Runnable mScrollToRegister = null;
					//mHandler.post(mScrollToRegister);
				}catch(Exception ex){
					lblError.setText(ex.toString());
				}
			}

						
		});
		
		/* Runnable mScrollToRegister = new Runnable(){
			
			public void run(){
				int off= mLayout.getMeasuredHeight()-sView.getHeight();
				if(off > 0){
					sView.scrollTo(0, off);
				}
			}
		};*/

		btnCancel.setOnClickListener(new Button.OnClickListener()
		{
			public void onClick(View v) {
				register.this.finish();
			}
		});

	}
	
	public void DisplayToast(String str)
 	{
 		Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
 	}
	
	@Override 
	public boolean onTouchEvent(MotionEvent evt)  
	{  
		if(evt.getAction() == MotionEvent.ACTION_DOWN)  
		{  
			getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
		}  
		return true;  
	}  	
	
}
